package com.yawn.zdweb.config;

import com.yawn.zdweb.controller.interceptor.IndexInterceptor;
import com.yawn.zdweb.controller.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;

/**
 * @author Created by yawn on 2018-01-26 11:06
 */
@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Resource
    private LoginInterceptor loginInterceptor;
    @Resource
    private IndexInterceptor indexInterceptor;
//    @Resource
//    private HeaderInterceptor headerInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
        super.addResourceHandlers(registry);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/user");
        registry.addViewController("/project");
        registry.addViewController("/defect");
        registry.addViewController("/about");
        registry.addViewController("/default");
        registry.addRedirectViewController("/","/default");
        super.addViewControllers(registry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 基于 WebRequest 的拦截器
        registry.addInterceptor(loginInterceptor);
        // 基于 HttpServletRequest 的拦截器
        registry.addInterceptor(indexInterceptor);
        // 获取session的id
//        registry.addInterceptor(headerInterceptor);
        super.addInterceptors(registry);
    }

//    class CustomMapper extends ObjectMapper {
//        public CustomMapper() {
//            this.setSerializationInclusion(JsonInclude.Include.NON_NULL);
//            // 设置 SerializationFeature.FAIL_ON_EMPTY_BEANS 为 false
//            this.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
//        }
//    }
//
//    @Bean
//    ObjectMapper objectMapper() {
//        return new CustomMapper();
//    }
}
